package arithmetic;

import java.io.Serializable;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import lombok.Data;

/**
 * @author jiangfeng
 * Created on 2020-03-12
 */
public class TestJsonUtils {

    public static void main(String[] args) throws JsonProcessingException {
        // 序列化为json,忽略null值
        ObjectMapper objectMapper = new ObjectMapper();
        // 加上按属性命名字母排序
        objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true);
        // json 里有map的时候,key也是按顺序.
        objectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        //objectMapper.registerModule(new JavaTimeModule());
        objectMapper.setSerializationInclusion(Include.NON_NULL);

        ImmutableMap<String, String> m = ImmutableMap.of("a", "1", "c", "3", "b", "2");
        A a= new A();
        a.setM(m);
        a.setS("123");
        a.setI(3);

        String s = objectMapper.writeValueAsString(a);
        System.out.println(s);

    }


    static class A implements Serializable {
        private Map m;
        private String s;
        private Integer i;

        public Map getM() {
            return m;
        }

        public void setM(Map m) {
            this.m = m;
        }

        public String getS() {
            return s;
        }

        public void setS(String s) {
            this.s = s;
        }

        public Integer getI() {
            return i;
        }

        public void setI(Integer i) {
            this.i = i;
        }
    }
}
